package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsEnergyChargeWindow;
import com.zhike.service.DwsEnergyChargeWindowService;
import com.zhike.mapper.DwsEnergyChargeWindowMapper;
import com.zhike.utils.BarUtils;
import com.zhike.utils.FormUtils;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_energy_charge_window(车辆充电信息表)】的数据库操作Service实现
* @createDate 2024-05-01 21:05:55
*/
@Service
public class DwsEnergyChargeWindowServiceImpl extends ServiceImpl<DwsEnergyChargeWindowMapper, DwsEnergyChargeWindow>
    implements DwsEnergyChargeWindowService{
    @Autowired
    private DwsEnergyChargeWindowMapper dwsEnergyChargeWindowMapper;

    @Override
    public String getChargeTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        List<String> carType = new ArrayList<>();
        List<Long> chargeCount = new ArrayList<>();
        List<Long> fully_charge_count = new ArrayList<>();
        List<Long> slow_charge_count = new ArrayList<>();
        List<Long> fast_charge_count = new ArrayList<>();
        List<Map> series = new ArrayList<>();

        try {
            List<Map> alarmTypeToday = dwsEnergyChargeWindowMapper.getChargeTypeWeek(date);
            if (!alarmTypeToday.isEmpty()) {
                for (Map map : alarmTypeToday) {
                    carType.add(map.get("car_type").toString());
                    chargeCount.add(Long.parseLong(map.get("charge_count").toString()));
                    fully_charge_count.add(Long.parseLong(map.get("fully_charge_count").toString()));
                    slow_charge_count.add(Long.parseLong(map.get("slow_charge_count").toString()));
                    fast_charge_count.add(Long.parseLong(map.get("fast_charge_count").toString()));
                }
            }

            series.add(Map.of(
                    "name","深度充",
                    "data",fully_charge_count
            ));
            series.add(Map.of(
                    "name","慢充",
                    "data",slow_charge_count
            ));

            series.add(Map.of(
                    "name","快充",
                    "data",fast_charge_count
            ));
            series.add(Map.of(
                    "name","总充电次数",
                    "data",chargeCount
            ));

            Map data = Map.of(
                    "categories", carType,
                    "series", series
            );
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getChargeBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgTypeToday = dwsEnergyChargeWindowMapper.getChargeBrandWeek(date);
            Map data = FormUtils.getChargeDeail(avgTypeToday,"品牌","brand");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getChargeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgTypeToday = dwsEnergyChargeWindowMapper.getChargeWeek(date);
            Map data = FormUtils.getChargeDeail(avgTypeToday,"汽车编码","vin");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getChargeTimeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergyChargeWindowMapper.getChargeTimeWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"vin","soc","charge_time","均充电电量","均充电时长");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getChargeTimeTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergyChargeWindowMapper.getChargeTimeTypeWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"car_type","soc","charge_time","均充电电量","均充电时长");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getChargeTimeBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergyChargeWindowMapper.getChargeTimeBrandWeek(date);
            Map data = BarUtils.getDetail(avgSpeedToday,"brand","soc","charge_time","均充电电量","均充电时长");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }
}




